

#include <iostream>
#include <stdlib.h>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/cstdint.hpp>
#include "protocol.h"



namespace rpc { namespace protocol {


serializer::serializer() 
: body_() 
{

}

serializer::serializer(const std::string& in_string) 
: body_() 
{
	from_string(in_string);
}

serializer::~serializer() {}

serializer::body_type& serializer::body() { return body_; }

bool serializer::from_string(const std::string& in_string) 
{
	std::istringstream in_stream;
    in_stream.str(in_string);
    boost::property_tree::json_parser::read_json(in_stream,  body_);
	
	return true;
}

void serializer::to_string(std::string& out_string) 
{
	std::ostringstream out_stream;
        boost::property_tree::json_parser::write_json(out_stream, body_);
		out_string = out_stream.str();
}


request::request() 
{
		
}

request::request(const std::string& in_string) 
: serializer(in_string)
{
		
}


request::~request() {}

int request::action() {	return body().get<int>("action"); }

request::parameters& request::params() { return body().get_child("parameters"); }

void request::set_action(int func_id) {
	body().put("action", func_id);
}

void request::set_params(const parameters& p) {
	body().put_child("parameters", p);
}

void request::dump() {
	std::string s;
	to_string(s);
	std::cout << "request:\n" << s << std::endl;
}



response::response() {}
response::~response(){}

void response::put_result_code(int code) {
	body().put("result_code", code);
}
	
int  response::get_result_code() {
	return body().get<int>("result_code");
}

void response::dump() {
	std::string s;
	to_string(s);
	std::cout << "response:\n" << s << std::endl;
}


event::event() {}
event::~event() {}


} } // namespace protocol } namespace rpc
